{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import getopt\n",
    "import argparse\n",
    "import random\n",
    "import boto3\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def setupClient(accessKey,secretKey,endpoint):\n",
    "    return boto3.client(\n",
    "        's3',\n",
    "        aws_access_key_id=accessKey,\n",
    "        aws_secret_access_key=secretKey,\n",
    "        use_ssl=True,\n",
    "        region_name='cn',\n",
    "        endpoint_url=endpoint\n",
    "    )\n",
    "\n",
    "def setupClients(accessKey,secretKey,endpoint,numClients):\n",
    "    clients=[]\n",
    "    for i in range(numClients):\n",
    "        clients.append(setupClient(accessKey,secretKey,endpoint))\n",
    "    return clients\n",
    "\n",
    "def runTest(opCode,clients,params):\n",
    "    random.shuffle(clients)\n",
    "    report={\n",
    "        'totalTransferred':0,\n",
    "        'totalDuration':0,\n",
    "        'numErrors':0,\n",
    "        'eachDuration':[]\n",
    "    }\n",
    "    t_test_begin=time.time()\n",
    "\n",
    "    for i in range(params[\"numSamples\"]):\n",
    "        t_iter_begin=time.time()\n",
    "        try:\n",
    "            if opCode=='write':\n",
    "                clients[i%params[\"numClients\"]].put_object(\n",
    "                    Bucket=params[\"bucket\"],\n",
    "                    Body=params[\"bufferBytes\"],\n",
    "                    Key=params[\"objectNamePrefix\"]+'_'+str(i)\n",
    "                )\n",
    "            elif opCode=='read':\n",
    "                clients[i%params[\"numClients\"]].get_object(\n",
    "                    Bucket=params[\"bucket\"],\n",
    "                    Key=params[\"objectNamePrefix\"]+'_'+str(i)\n",
    "                )\n",
    "            t_iter_end=time.time()\n",
    "            report['eachDuration'].append(t_iter_end-t_iter_begin)\n",
    "            report['totalTransferred']+=params['objectSize']\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "            report['numErrors']+=1\n",
    "\n",
    "    t_test_end=time.time()\n",
    "    report['totalDuration']=t_test_end-t_test_begin\n",
    "    if opCode=='write':\n",
    "        params['writingTestReport']=report\n",
    "    elif opCode=='read':\n",
    "        params['readingTestReport']=report\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def showTestParameters(params):\n",
    "    print(\n",
    "        \"endpoint(s):\\t\\t[%s]\\n\" % params[\"endpoint\"]+\n",
    "        \"bucket:\\t\\t\\t%s\\n\" % params[\"bucket\"]+\n",
    "        \"objectNamePrefix:\\t%s\\n\" %params[\"objectNamePrefix\"]+\n",
    "        \"objectSize:\\t\\t%.2fKB\\n\" % (params[\"objectSize\"]/2**10)+\n",
    "        \"numClients:\\t\\t%d\\n\" % params[\"numClients\"]+\n",
    "        \"numSamples:\\t\\t%d\\n\" % params[\"numSamples\"]\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def main(argv):\n",
    "    \n",
    "    params={\"accessKey\":'',\"secretKey\":'',\"bucket\":'',\"endpoint\":'',\n",
    "    \"numClients\":'',\"numSamples\":'',\"objectNamePrefix\":'',\"objectSize\":''}\n",
    "\n",
    "    help=\"MyS3Bench.py\\n\\\n",
    "    --accessKey=<Access Key>\\n\\\n",
    "    --accessSecret=<Secret Key>\\n\\\n",
    "    --bucket=<Bucket Name>\\n\\\n",
    "    --endpoint=<http://IP:PORT>\\n\\\n",
    "    --numClients=<Num Of Clients>\\n\\\n",
    "    --numSamples=<Num Of Samples>\\n\\\n",
    "    --objectNamePrefix=<Object Name Prefix>\\n\\\n",
    "    --objectSize=<Size Of Object>\"\n",
    "\n",
    "    try:\n",
    "        opts, args = getopt.getopt(argv,\"h\",[\"accessKey=\",\"secretKey=\",\"bucket=\",\n",
    "        \"endpoint=\",\"numClients=\",\"numSamples=\",\"objectNamePrefix=\",\"objectSize=\"])\n",
    "    except getopt.GetoptError:\n",
    "        print(\"Error Arguments! The help is folloing:\\n\"+help)\n",
    "        sys.exit(-1)\n",
    "    for opt,arg in opts:\n",
    "        if(opt)=='-h':\n",
    "            print(help)\n",
    "            sys.exit(0)\n",
    "        else:\n",
    "            params[opt[2:]]=arg\n",
    "    params[\"numClients\"],params[\"numSamples\"],params[\"objectSize\"]=\\\n",
    "        int(params[\"numClients\"]),int(params[\"numSamples\"]),int(params[\"objectSize\"])\n",
    "    ##print(params)\n",
    "\n",
    "    if params[\"numClients\"]>params[\"numSamples\"] or params[\"numClients\"]<=0:\n",
    "        print(\"numClients %d needs to be less than numSamples(%d) and greater than 0\\n\" % (params[\"numClients\"],params[\"numSamples\"]))\n",
    "        sys.exit(-1)\n",
    "\n",
    "    print(\"Generating in-memory sample data...\")\n",
    "    t_genSample_begin=time.time()\n",
    "    params[\"bufferBytes\"]=bytes(params[\"objectSize\"])\n",
    "    t_genSample_end=time.time()\n",
    "    print(\"Done (%fms)\\n\" % (t_genSample_end-t_genSample_begin))\n",
    "\n",
    "    print(\"Setup %d clients connected with the server(%s)\\n\" % (params[\"numClients\"],params[\"endpoint\"]))\n",
    "    clients=setupClients(params[\"accessKey\"],params[\"secretKey\"],params[\"endpoint\"],params[\"numClients\"])\n",
    "    ##print(s3)\n",
    "\n",
    "    ##print(s3.list_buckets())\n",
    "\n",
    "    print(\"Running Writing test...\")\n",
    "    runTest('write',clients,params)\n",
    "    print('Done\\n')\n",
    "    \n",
    "    print(\"Running Reading test...\")\n",
    "    runTest('read',clients,params)\n",
    "    print('Done\\n')\n",
    "\n",
    "\n",
    "\n",
    "    showTestParameters(params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abab'.startswith('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-33-5af348ea58b2>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-33-5af348ea58b2>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    123.startwith('1')\u001b[0m\n\u001b[1;37m        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "7b4b4feff2f24a0f0a34464dbe537a36fda679851528fb8735cb41fa49dffb2d"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
